package com.bangcommunity.bbframe.sdm.web.shiro.stateless;

import com.bangcommunity.bbframe.common.utils.lang.StringUtils;
import com.bangcommunity.bbframe.sdm.utils.RequestUtils;
import com.bangcommunity.bbframe.sdm.web.exception.RestConf;
import com.bangcommunity.bbframe.sdm.web.shiro.ISdmWebTokenService;
import com.bangcommunity.bbframe.sdm.web.shiro.SdmUidTokenAuthenticationToken;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.Serializable;

/**
 * @Author tanghc
 * @Date 17/11/16
 */
public class SdmUidTokenStatelessAuthcFilter<PK extends Serializable>
        extends AbstractSdmStatelessAuthenticationFilter<PK, SdmUidTokenAuthenticationToken> {
    @Autowired(required = false)
    private ISdmWebTokenService<PK> sdmWebTokenService;

    @Override
    protected SdmUidTokenAuthenticationToken createSdmToken(HttpServletRequest request, HttpServletResponse response) {
        String tokenParam = RequestUtils.extractParam(request, RestConf.tokenKey);
        String uidParam = RequestUtils.extractParam(request, RestConf.uidKey);
        if (StringUtils.isBlank(tokenParam)) {
            logger.error("create sdm token fail no token");
            return null;
        }
        // if(StringUtils.isBlank(uidParam)){
        // logger.error("create sdm uid fail no username");
        // return null;
        // }
        SdmUidTokenAuthenticationToken token = new SdmUidTokenAuthenticationToken();
        token.setSsoToken(tokenParam);
        token.setUid(uidParam);
        return token;
    }

}
